<?php
/**
 * Created by PhpStorm.
 * User: EZ
 * Date: 2017/6/6
 * Time: 14:25
 */
namespace app\index\controller;
use core\Route;
//include ROOT_PATH.'vendor/autoload.php';
class Server extends Route
{
    public $ser = null;
    public $url = null;
    public $res = null;

    public function __construct()
    {
        $this->ser = self::hproseServer();
        $a = $this->getMethods(__CLASS__);

        foreach($a as $v){
            if($v !='__construct' && $v!='__destruct'){
                $this->ser->addMethod($v, $this);
            }
        }
        $this->ser->start();
    }

    public function hello($word = '')
    {
        return  '你好--' . $word;
    }

    public function test($word = '')
    {
        return "这是测试数据--" . $word;
    }

    public function __destruct()
    {
        $this->ser = null;
    }

    /**
     * @param $classname
     * @param null $access
     * @return array
     * 只获取本类的方法名，不包括父级方法名，
     */
    public function getMethods($classname, $access = null)
    {
        $class = new \ReflectionClass($classname);

        $methods = $class->getMethods();

        $returnArr = array();
        foreach ($methods as $value) {
            if ($value->class == $classname) {
                if ($access != null) {
                    $methodAccess = new \ReflectionMethod($classname, $value->name);
                    switch ($access) {
                        case 'public':
                            if ($methodAccess->isPublic()) $returnArr[$value->name] = 'public';
                            break;
                        case 'protected':
                            if ($methodAccess->isProtected()) $returnArr[$value->name] = 'protected';
                            break;
                        case 'private':
                            if ($methodAccess->isPrivate()) $returnArr[$value->name] = 'private';
                            break;
                        case 'final':
                            if ($methodAccess->isFinal()) $returnArr[$value->name] = 'final';
                            break;
                    }
                } else {
                    array_push($returnArr, $value->name);
                }

            }
        }
        return $returnArr;
    }

    /*  protected function exe($ar=[]){
       $this->ser = self::hproseServer();
        foreach($ar as $v){
            if($v !='__construct' && $v!='__destruct'){
                $this->ser->addMethod($v, $this);
            }
        }
        $this->ser->start();
    }*/

    /**
     * @param $classname
     * @param null $access
     * @return array
     * 只获取本类的方法名，不包括父级方法名，
     */
    /*  public function getMethods($classname,$class_ar=[], $access = null)
      {
          if($class_ar && is_array($class_ar)){
              $class_ar=array_merge([__CLASS__],$class_ar);
          }
          $class = new \ReflectionClass($classname);
          $methods = $class->getMethods();

          $returnArr = array();
          foreach ($methods as $value) {
              if($class_ar && is_array($class_ar)) {
                  if (in_array($value->class, $class_ar)) {
                      if ($access != null) {
                          $methodAccess = new \ReflectionMethod($classname, $value->name);
                          switch ($access) {
                              case 'public':
                                  if ($methodAccess->isPublic()) $returnArr[$value->name] = 'public';
                                  break;
                              case 'protected':
                                  if ($methodAccess->isProtected()) $returnArr[$value->name] = 'protected';
                                  break;
                              case 'private':
                                  if ($methodAccess->isPrivate()) $returnArr[$value->name] = 'private';
                                  break;
                              case 'final':
                                  if ($methodAccess->isFinal()) $returnArr[$value->name] = 'final';
                                  break;
                          }
                      } else {
                          array_push($returnArr, $value->name);
                      }
                  }
              }elseif($value->class == $classname) {
                  if ($access != null) {
                      $methodAccess = new \ReflectionMethod($classname, $value->name);
                      switch ($access) {
                          case 'public':
                              if ($methodAccess->isPublic()) $returnArr[$value->name] = 'public';
                              break;
                          case 'protected':
                              if ($methodAccess->isProtected()) $returnArr[$value->name] = 'protected';
                              break;
                          case 'private':
                              if ($methodAccess->isPrivate()) $returnArr[$value->name] = 'private';
                              break;
                          case 'final':
                              if ($methodAccess->isFinal()) $returnArr[$value->name] = 'final';
                              break;
                      }
                  } else {
                      array_push($returnArr, $value->name);
                  }
              }
          }
          return $returnArr;
      }*/

}
